<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 04/26/2012
 * @project		: Drag & Drop
 * @description	: 
 *		- 
 *
\* ************************************************** */

class Functions extends MY_Controller{
    function __construct( ){
        parent::__construct( );  
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load->helper('date');
    }
	
	function index(){
        
    } 
	function check_login(){
		$User_ID=$this->input->post('tendangnhap');
		$Password=$this->input->post('matkhau');
		
		$result=$this->CodeGenerator_model->get('employee','*',array('Personal_ID'=>$User_ID,'Password'=>$Password,'delete'=>0,'active'=>1),1,0,true);
		$data['login']=0;
		$data['error']="";
		$data['url_login']="";
		if(empty($result)) $data['error']="Tên đăng nhập hay mật khẩu không đúng.";
		else {
			$data['login']=1;
			$this->setSession($result);
		}
		/*
		if(!empty($result)){//user not yet active
			$data['error']="Please reactive the account.";
		}else{
			$result=$this->CodeGenerator_model->get('users','*',array('username'=>$User_ID,'enabled'=>1),1,0,true);			
			if(!empty($result)){// user existed
				//check password
				$decode=$this->encrypt->decode($result->password, SECRET_KEY_PASSWORD);
				if($decode!=$Password) $data['error'] = "User or Password is invalid";
				else{
					//set session user login
					$this->MYC_setSession($result);
					$data['login']=1;
					$data['url_login']=$this->session->userdata('url_cur_login');
					$this->MYC_unsetSessionUrl();
				}
			}else{ // user invalid
				$data['error']= "User or Password is invalid";
			}
		}
		*/
		echo json_encode($data);
		exit();
	}
	
	function check_pass(){
		$User_ID=$this->input->post('tendangnhap');
		$Password=$this->input->post('matkhau');
		$gotourl=$this->input->post('gotourl');
		
		$result=$this->CodeGenerator_model->get('employee','*',array('Personal_ID'=>$User_ID,'Password'=>$Password,'delete'=>0,'active'=>1),1,0,true);
		$data['login']=0;
		$data['error']="";
		$data['gotourl']=$gotourl;
		if(empty($result)) $data['error']="Tên đăng nhập hay mật khẩu không đúng.";
		else {
			$data['login']=1;
		}
		echo json_encode($data);
		exit();
	}
	function logout(){
		$this->session->sess_destroy();
		redirect(base_url());
	}
	
	// json for mobile 
	
	/*
	* getDanhMuc
	* params : none
	* return json encode : danh sach danh muc
	* order by Name
	*/
	function getDanhMuc(){
		$i =  $this->uri->segment( 3 ); 		
		$n =  $this->uri->segment( 4 ); 
		$limit = "";
		if(!empty($i) || ($i >=0&& !empty($n))) $limit = "limit $i,$n";
		$sql = "select * from categories order by Name $limit";
		$results=$this->CodeGenerator_model->custom_query($sql,"object");
		$data['categories'] = $results;
		//get total 
		$sql = "select count(Ca_ID) as total from categories";
		$total = $this->CodeGenerator_model->custom_query($sql,"array");
		$data['total'] = $total[0]['total'];
		echo json_encode($data);
		exit();
	}
	
	/*
	* getServices
	* params : danhmuc id
	* return json encode :
					object(
							'...'=>'....'
							 .........
							'services'=>danh sach dich vu thuoc danh muc do
							)
	* order by Name service
	*/
	function getServices(){
		$cat_id =  $this->uri->segment( 3 ); 
		$i =  $this->uri->segment( 4 ); 		
		$n =  $this->uri->segment( 5 ); 
		$limit = "";
		if(!empty($i) || ($i >=0&& !empty($n))) $limit = "limit $i,$n";
		if(!empty($cat_id)){
			$data=$this->CodeGenerator_model->get('categories','*',array('Ca_ID'=>$cat_id),1,0,true);
			$sql = "select * from services where cat_id = $cat_id order by Serv_Name $limit";
			$results=$this->CodeGenerator_model->custom_query($sql,"object");
			$data->services = $results;
			//get total 
			$sql = "select count(Serv_ID) as total from services where cat_id = $cat_id";
			$total = $this->CodeGenerator_model->custom_query($sql,"array");
			$data->total = $total[0]['total'];
		}else{
			$data = null;
		}
		echo json_encode($data);
		exit();
	}
	
	/*
	* getInfoService
	* params : serv_id
	* return json encode :
					object service item(
							'...'=>'....'
							)
	*/
	function getInfoService(){
		$serv_id =  $this->uri->segment( 3 ); 
		if(!empty($serv_id)){
			$data=$this->CodeGenerator_model->get('services','*',array('Serv_ID'=>$serv_id),1,0,true);
		}else{
			$data = null;
		}
		echo json_encode($data);
		exit();
	}
	
	//end json for mobile
}

/* End of file index.php */
/* Location: ./index.php */